package com.xj.product.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xj.product.mapper.ProductMapper;
import com.xj.product.domain.Product;
import com.xj.product.service.ProductService;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService{
    @Autowired
    private ProductMapper productMapper;
    @Override
    public Product findByPid(Integer pid) {
        return productMapper.selectById(pid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reduceInventory(Integer pid, int num) {
        Product product = productMapper.selectById(pid);
        if (product.getNum() < num) {
            throw new RuntimeException("库存不足");
        }
//        int i = 1 / 0;
        product.setNum(product.getNum() - num);
        productMapper.updateById(product);
    }
}
